package j2024.j202408;

public class j0828 {
    /**
     * 733. 图像渲染
     */
    int[] dx = {0,0,-1,1};
    int[] dy = {1,-1,0,0};
    int m,n;
    int prev;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        if(image[sr][sc]==color){
            return image;
        }
        m = image.length;
        n = image[0].length;
        prev = image[sr][sc];
        dfs(image,sr,sc,color);
        return  image;
    }
    public void dfs(int[][] image,int sr,int sc,int color){
        image[sr][sc] = color;
        for (int i = 0; i < 4; i++) {
            int x = sr+dx[i],y = sc+dy[i];
            if(x>=0 && x<m && y>=0 && y<n && image[x][y]==prev){
                dfs(image,x,y,color);
            }
        }
    }
}
